//
//  DMRefreshView.swift
//  DM_新浪微博
//
//  Created by Damon.JiAo on 16/3/7.
//  Copyright © 2016年 Demon_JiAo. All rights reserved.
//

import UIKit

/** 设置一个枚举类型 */
enum RefreshType:Int{
    
    case Normal = 0
    /// 拖拽状态
    case Pulling = 1
    /// 加载状态
    case Loading = 2
    
}


class DMRefreshView: UIControl {
    
    /** 成员变量 */
    var scrollView:UIScrollView?
    /** 记录一下之前的状态 */
    var oldStatus:RefreshType = .Normal
    
    
    var status:RefreshType = .Normal{
        
        didSet{
            switch status{
                
            case .Normal:
                
                messageLabel.text = "下拉刷新"
                
            case .Pulling:
                
                messageLabel.text = "释放更新"
                
            case .Loading:
                
                messageLabel.text = "加载中...."
                var inset = scrollView?.contentInset
                inset?.top += 64
                scrollView?.contentInset = inset!
                
                sendActionsForControlEvents(UIControlEvents.ValueChanged)
                oldStatus = .Loading
            }
        }
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUp()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUp(){
        
        var SFrame = self.frame
        
        SFrame.size = CGSizeMake(KUIScreenWidth, 50)
        SFrame.origin.y = -50
        
        self.frame = SFrame
        
        addSubview(messageLabel)
        addSubview(arrowImageView)
        addSubview(loadingImageView)
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)//.offset(30)
            make.centerY.equalTo(self.snp_centerY)
        }
        
        arrowImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(self.snp_centerY)
            make.right.equalTo(messageLabel.snp_left)
        }
        
        loadingImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(arrowImageView.snp_center)
        }
    }
    
    /** 添加KVO */
     // 把子控件添加到父控件的时候会调用此方法
     // newSuperview是 UITableView
    override func willMoveToSuperview(newSuperview: UIView?) {
        super.willMoveToSuperview(newSuperview)
        
        scrollView = newSuperview as?UIScrollView
        
        scrollView?.addObserver(self, forKeyPath: "contentOffset", options: NSKeyValueObservingOptions.New, context: nil)
    }
    
    /**  */
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if keyPath == "contentOffset"{
        }
        
        let scrollView = object as? UIScrollView
        //        print(scrollView?.contentOffset.y)
        // 给 scrollView一个强制 ,建议大家不用强转
        //第三步: 确定了 三种状态
        
        if scrollView!.dragging{    // 判断是否为拖拽的时候
            
            if scrollView?.contentOffset.y < -150{
                //  更改状态
                status = .Pulling
                //  此处调用一个方法改变视图
                
            }else if scrollView?.contentOffset.y < -114 && scrollView?.contentOffset.y > -150{
                //  更改状态
                status = .Normal
                //  此处调用一个方法改变视图
            }
            
        }else{
            
            if scrollView?.contentOffset.y < -150 && status == .Pulling{
                //  更改状态
                status = .Loading
                //  此处改变
            }
            
        }
    }
    
    func endRefreshing(){
        
        status = .Normal
        
        if oldStatus == .Loading{
            
            var inset = scrollView?.contentInset
            inset?.top -= 64
            scrollView?.contentInset = inset!
            
            oldStatus = .Normal
        }
        
    }
    
    
    ///     加载文字提示
    private lazy var messageLabel:UILabel = {
        
        let label = UILabel()
        
        label.text = "下拉刷新"
        label.textColor = UIColor.redColor()
        label.font = UIFont.systemFontOfSize(15)
        
        return label
    }()
    
    ///     箭头
    private lazy var arrowImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "tableview_pull_refresh")
        
        return img
    }()
    
    ///     加载图片
    private lazy var loadingImageView:UIImageView = {
    
    
        let img = UIImageView()
        
        img.image = UIImage(named: "tableview_loading")
        
        img.hidden = true
    
        return img
    }()
    
}
